package org.example.diary.controller;

import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.example.diary.entity.dto.DiaryAddDTO;
import org.example.diary.entity.vo.DiaryVO;
import org.example.diary.entity.vo.Response;
import org.example.diary.service.DiaryService;
import org.example.diary.validator.SaveGroup;
import org.example.diary.validator.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
public class DiaryController {

    @Resource
    private DiaryService diaryService;

    @PreAuthorize("hasRole('user')")
    @PostMapping("diary_add")
    public Response<String> diaryAdd(@RequestBody @Validated({SaveGroup.class}) DiaryAddDTO diaryAddDTO){
        return Response.ok(diaryService.diaryAdd(diaryAddDTO));
    }

    @PreAuthorize("hasRole('user')")
    @PostMapping("diary_update")
    public Response<String> diaryUpdate(@RequestBody @Validated({UpdateGroup.class}) DiaryAddDTO diaryAddDTO){
        return Response.ok(diaryService.diaryUpdate(diaryAddDTO));
    }

    @PreAuthorize("isAuthenticated()")
    @GetMapping("diary_list")
    public Response<PageInfo<DiaryVO>> diaryList(Integer pageNum, Integer pageSize){
        return Response.ok(diaryService.diaryList(pageNum,pageSize));
    }

    @PreAuthorize("hasRole('user')")
    @PostMapping("diary_del/{diaryId}")
    public Response<String> diaryDel(@PathVariable Integer diaryId){
        return Response.ok(diaryService.diaryDel(diaryId));
    }

    @PreAuthorize("hasRole('admin')")
    @PostMapping("diary_hide/{diaryId}")
    public Response<String> diaryHide(@PathVariable Integer diaryId){
        return Response.ok(diaryService.diaryHide(diaryId));
    }


}
